---智能家居模型
--@module
local githook = class('githook', require('framework.cls.basehandler'))
--======================================
--default params
local d = Ding
local Logger = d.Logger
--===================================

local function safe_get_sshurl(resp, property)
    local ssh_url
    try{
        function()
            ssh_url = resp["project"]["git_ssh_url"]
        end
    }
    return ssh_url
end

function githook:ctor(svr)
    githook.super.ctor(self, 'githook', svr)
    d.bind(self, 'base_decorator')

    self._ssh_handlers = {
        require("api.handler.hookmap.xlwork"),
        require("api.handler.hookmap.gitee"),
    }
end

function githook:get_gitlab_vo()
    local ok, reqdata = self:body_jsonparams_nec_()
    if not ok then
        Logger:error("json body decode raise error")
        return
    end
    return reqdata
end

function githook:gitlab_handler()
    local resp = self:get_gitlab_vo()
    Logger:dump(resp, "-->>resp gitlab hook")
    local ssh_url = safe_get_sshurl(resp)
    Logger:info("-->>ssl url =%s", ssh_url)
    if resp and ssh_url then 
        for _, handler in ipairs(self._ssh_handlers) do 
            if handler["ssh_url"] == ssh_url then 
                func_hook(handler, resp["hook_name"], resp)
            end
        end
    else 
        Logger:error("Hook response format is out of wanted")
    end 
end

return githook
